//
//  CityListViewController.m
//  51-Tour
//
//  Created by xiang ying on 14-3-20.
//  Copyright (c) 2014年 xiang ying. All rights reserved.
//

#import "CityListViewController.h"
#import "DownloadViewController.h"
#import "ServiceManage.h"
#import "SVProgressHUD.h"

@interface CityListViewController ()<UITableViewDataSource,UITableViewDelegate>{
    NSArray *_itemsArray;
    UITableView *menu;
    
    language_type   _languageType;
    
    DownloadViewController *_downloadView;
}

@end

@implementation CityListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)popself{
    [super popself];
    [[UIManagement shareInstance] showLeft];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initNavigationItme:[[DataCenter instance] getStringForKey:@"CityList"]];
//    _itemsArray = @[@"北京",@"西安",@"南京",@"洛阳"];
    
    menu = [[UITableView alloc] initWithFrame:self.contentView.bounds];
    menu.separatorStyle = UITableViewCellSeparatorStyleNone;
    [menu setBackgroundColor:[UIColor clearColor]];
    menu.dataSource = self;
    menu.delegate = self;
    [self.contentView addSubview:menu];
    
    _languageType = unknow;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if (_languageType != [[DataCenter instance] getLanguageType]) {
        _itemsArray = nil;
        [menu reloadData];
        //needreload
        NSDictionary *requestinfo = @{@"Mobile": [DataCenter instance].user.userPhone,@"LanKey":@"en-us"};
        [self initNavigationItme:[[DataCenter instance] getStringForKey:@"CityList"]];
        if ([[DataCenter instance] getLanguageType] == zh_cn) {
            requestinfo = @{@"Mobile": [DataCenter instance].user.userPhone,@"LanKey":@"zh-cn"};
        }
        
        [SVProgressHUD showWithStatus:[[DataCenter instance] getStringForKey:@"loading"]];
        [ServiceManage getCitylistRequestInfo:requestinfo completion:^(RespondDTO *respond) {
            [SVProgressHUD dismiss];
            if (respond.flag == respond_success) {
                //
                _languageType = [[DataCenter instance] getLanguageType];
                _itemsArray = respond.data;
                [menu reloadData];
            }else{
                [SVProgressHUD showErrorWithStatus:TIP_ERROR];
                [self performSelector:@selector(popself) withObject:nil afterDelay:0.5];
            }
        }];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - UITableViewDataSource & UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _itemsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.backgroundColor = [UIColor clearColor];
        
        UIImageView *_line = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"list-line_14"]];
        _line.frame = CGRectMake(0, 43, 320, 1);
        [cell addSubview:_line];
    }
    
    CityDTO *dto = _itemsArray[indexPath.row];
    cell.textLabel.text = dto.cityName;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *label = [[UILabel alloc] init];
    label.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.8];
    label.text = [[DataCenter instance] getStringForKey:@"CurrentCitylist"];// @"   目前已开通城市";
    label.font = [UIFont systemFontOfSize:13];
    label.textColor = [UIColor darkGrayColor];
    return label;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (!_downloadView) {
        _downloadView = [[DownloadViewController alloc] init];
    }
    [_downloadView setCity:_itemsArray[indexPath.row]];
    [self.navigationController pushViewController:_downloadView animated:YES];
}
@end
